﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Erebos.ScreenFiles;
using Lidgren.Network;

namespace Erebos.MainMenuScreenFiles
{
    class MainMenuScreen : Screen
    {
        private TextBox _textBox;
        private TextBox _passwordBox;
        private Button _button;
        private MessageBox _box;

        public MainMenuScreen()
        {
            _textBox = new TextBox(new Vector2(200, 200), 16, MainGame.Fonts["Arial12"]);
            _passwordBox = new TextBox(new Vector2(200, 200), 16, MainGame.Fonts["Arial12"]);
            _button = new Button("Login", "Arial12", MainGame.Textures["Button"], new Vector2(200, 200), new Vector2(25, 21));
            _button.OnClick += Button_OnClick;
        }

        private void Button_OnClick(object sender)
        {
            _box = new MessageBox("Verbindung zum Server wird hergestellt.");
            SendCollection<object, VariableType> collection = new SendCollection<object, VariableType>();
            collection.Add((byte)0x01, VariableType.Byte);
            collection.Add(_textBox.Text, VariableType.String);
            collection.Add(_passwordBox.Text, VariableType.String);
            ClientHandler.Send(collection, NetDeliveryMethod.Unreliable);
            ClientHandler.AddHandle(0x01, OnIncoming_0x01);
        }

        /// <summary>
        /// Answere of charackter's request
        /// </summary>
        /// <param name="message">The sended request</param>
        private void OnIncoming_0x02(NetIncomingMessage message)
        {
            List<Charackter> foundedChars = new List<Charackter>();
            string charName = message.ReadString();
            while (!String.IsNullOrEmpty(charName))
            {
                int level = message.ReadInt32();
                int charcode = message.ReadInt32();
                string rights = message.ReadString();
                int mindex = message.ReadInt32();
                foundedChars.Add(new Charackter(charName, level, charcode, rights, mindex));
                Console.WriteLine(charName);
                try
                {
                    charName = message.ReadString();
                }
                catch (Exception)
                {
                    break;
                }
            }

            MainGame.SetScreen(new CharackterScreenFiles.CharackterSelectScreen(foundedChars));

        }

        /// <summary>
        /// Answere of login request
        /// </summary>
        /// <param name="message">The sended request</param>
        private void OnIncoming_0x01(NetIncomingMessage message)
        {
            bool succes = message.ReadBoolean();
            if (succes)
            {
                _box = new MessageBox("Lade Spielinhalt.");
                MainGame.LoadGameContent();
                SendCollection<object, VariableType> collection = new SendCollection<object, VariableType>();
                collection.Add((byte)0x02, VariableType.Byte);
                ClientHandler.Send(collection, NetDeliveryMethod.Unreliable);
                ClientHandler.AddHandle(0x02, OnIncoming_0x02);
            }
            else
                _box = new MessageBox("Fehlerhafter Benutzername oder Passwort.", true);
        }

        public override void Update(GameTime gameTime)
        {
            _textBox.Update();
            _passwordBox.Update();
            _button.Update();

            if (_box != null)
                _box.Update();
            base.Update(gameTime);
        }

        public override void Draw(SpriteBatch spriteBatch, GameTime gameTime)
        {
            spriteBatch.Draw(MainGame.Textures["Blood"], new Rectangle(0, 0, 100, 61),Color.White);
            spriteBatch.Draw(MainGame.Textures["Blood"], new Rectangle(ScreenSizeHelper.SizeWidth - 100, 0, 100, 61), Color.White);
            spriteBatch.Draw(MainGame.Textures["Blood"], new Rectangle(0, ScreenSizeHelper.SizeHeight - 61, 100, 61), Color.White);
            spriteBatch.Draw(MainGame.Textures["Blood"], new Rectangle(ScreenSizeHelper.SizeWidth - 100, ScreenSizeHelper.SizeHeight - 61, 100, 61), Color.White);
            spriteBatch.Draw(MainGame.ErebosLogo, new Vector2(ScreenSizeHelper.SizeWidth / 2 - MainGame.ErebosLogo.Width / 2, ScreenSizeHelper.GetPixelVertical(10)), Color.White);
            Vector2 pos = new Vector2((ScreenSizeHelper.SizeWidth / 2) - ((MainGame.Fonts["Arial12"].MeasureString("Benutzername:").X + MainGame.Textures["TextBox"].Width) / 2), ScreenSizeHelper.GetPixelVertical(10) + MainGame.ErebosLogo.Height + ScreenSizeHelper.GetPixelVertical(15, MainGame.ErebosLogo.Height));
            _textBox.Location = new Vector2(pos.X + MainGame.Fonts["Arial12"].MeasureString("Benutzername:").X, pos.Y - 20);
            spriteBatch.DrawString(MainGame.Fonts["Arial12"], "Benutzername:", pos, Color.DarkRed);
            _textBox.Draw(spriteBatch);
            Vector2 pos2 = new Vector2(MainGame.Fonts["Arial12"].MeasureString("Benutzername:").X - MainGame.Fonts["Arial12"].MeasureString("Passwort:").X + pos.X, pos.Y + 66);
            _passwordBox.Location = new Vector2(pos.X + MainGame.Fonts["Arial12"].MeasureString("Benutzername:").X, pos.Y + 46);
            spriteBatch.DrawString(MainGame.Fonts["Arial12"], "Passwort:", pos2, Color.DarkRed);
            _passwordBox.Draw(spriteBatch);
            Vector2 pos3 = new Vector2(pos.X + MainGame.Fonts["Arial12"].MeasureString("Benutzername:").X, pos.Y + 96);
            _button.Position = pos3;
            _button.Draw(spriteBatch);
            spriteBatch.DrawString(MainGame.Fonts["Arial10"], "© Jonas Bernemann", new Vector2(105, ScreenSizeHelper.SizeHeight - MainGame.Fonts["Arial10"].MeasureString("©Jonas Bernemann").Y - 5), Color.White);

            if (_box != null)
                _box.Draw(spriteBatch);
            base.Draw(spriteBatch, gameTime);
        }
    }
}
